<template>
  <div class="forum-list-wrap">
    <div class="banner">
      <el-carousel height="5.5rem">
        <el-carousel-item v-for="(item, index) in bannerList" :key="index">
          <div class="banner-img" @click="clickBannerFun(item)">
            <img :src="item.image" alt="" />
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="m-cagegory">
      <div class="m-tab">
        <div
          class="m-tab-item"
          :class="{ 'm-tab-item--active': tabCurrent == index }"
          @click="selectTabFun(item, index, $event)"
          v-for="(item, index) in tabList"
          :key="index"
        >
          {{ item.title }}
        </div>
      </div>
      <!-- <div class="push" @click="pushFun">发布</div> -->
    </div>

    <div class="list">
      <NoticeItem v-for="(item, index) in list" :item="item" :key="index"></NoticeItem>
    </div>
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="paginationFun"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import NoticeItem from "./pageComponents/notice-item.vue";

import { getNoticeList, getNoticeTab } from "@/api/notice";
import { getBanner } from "@/api/common";
export default {
  components: {
    NoticeItem,
  },
  data() {
    return {
      bannerList: [],
      params: {
        page: 1,
        limit: 10,
        type_id: ''
      },
      list: [],
      total: 0,

      //
      tabList: [],
      tabCurrent: 0,
    };
  },
  created() {
    this.setView();
  },
  mounted() {
    this.getBannerFun();
    this.getListFun();
  },
  methods: {
    setView() {
      getNoticeTab().then((res) => {
        console.warn("公告分类", res);
        this.tabList = [
          {
            id: 0,
            title: "全部",
          },
          ...res.data,
        ];
      });
    },

    selectTabFun(item, index, event) {
      if (this.tabCurrent == index) {
        return;
      }
      this.tabCurrent = index;
      this.params.type_id = item.id;
      this.params.page = 1;
      this.getListFun();
    },

    getBannerFun() {
      getBanner({
        position: 8
      }).then((res) => {
        this.bannerList = res.data;
      });
    },
    clickBannerFun(item) {
      if (item.url) {
        window.location.href = item.url;
      }
    },
    paginationFun(page) {
      this.params.page = page;
      this.getListFun();
    },
    getListFun() {
      getNoticeList(this.params).then((res) => {
        if (res.code == 200) {
          this.total = res.data.count;
          this.list = res.data.list;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.forum-list-wrap {
  .banner {
    width: 100%;
    height: 5.5rem;
    .banner-img {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .m-cagegory {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 1rem;
    background: rgba(79, 138, 248, 0.1);
    margin-top: 0.4rem;
    box-sizing: border-box;
    padding: 0 0.5rem 0 0;
    .push {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 1.5rem;
      height: 0.5rem;
      background: #4f8af8;
      border-radius: 0.1rem;
      font-size: 0.2rem;
      color: #fff;
      cursor: pointer;
      user-select: none;
    }
    .m-tab {
      display: flex;
      width: 100%;
      height: 100%;
      overflow-x: scroll;
      &::before {
        content: "";
        display: block;
        width: 0.125rem;
        height: 100%;
      }
      &::-webkit-scrollbar {
        height: 0;
        border-radius: 0.025rem;
        background: rgba(0, 0, 0, 0.1);
      }
      &::-webkit-scrollbar-thumb {
        background: #4f8af8;
      }
      .m-tab-item {
        display: flex;
        align-items: center;
        padding: 0 0.375rem;
        font-size: 0.3rem;
        color: #333;
        flex-shrink: 0;
        height: 100%;
        cursor: pointer;
        user-select: none;
      }
      .m-tab-item--active {
        font-weight: 800;
      }
    }
  }

  .list {
    padding-top: 0.5rem;
  }
  .pagination {
    display: flex;
    justify-content: center;
    padding: 0.375rem 0 1rem;
  }
}

// 新增

.m-cagegory {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 1rem;
  background: rgba(79, 138, 248, 0.1);
  margin-top: 0.4rem;
  box-sizing: border-box;
  padding: 0 0.5rem 0 0;
  .push {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 0.5rem;
    background: #4f8af8;
    border-radius: 0.1rem;
    font-size: 0.2rem;
    color: #fff;
    cursor: pointer;
    user-select: none;
  }
  .m-tab {
    display: flex;
    width: 15rem;
    height: 100%;
    overflow-x: scroll;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    scroll-duration: 1s;
    &::before {
      content: "";
      display: block;
      width: 0.125rem;
      height: 100%;
    }
    &::-webkit-scrollbar {
      height: 0;
      border-radius: 0.025rem;
      background: rgba(0, 0, 0, 0.1);
    }
    &::-webkit-scrollbar-thumb {
      background: #4f8af8;
    }
    .m-tab-item {
      display: flex;
      align-items: center;
      padding: 0 0.375rem;
      font-size: 0.3rem;
      color: #333;
      flex-shrink: 0;
      height: 100%;
      cursor: pointer;
      user-select: none;
    }
    .m-tab-item--active {
      font-weight: 800;
    }
  }
}
</style>
